<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/25 17:27
 * Modified Time: 2019/7/25 17:27
 * Version: 2.0
 */

namespace App\Modules\Partner\Repositories;


use App\Models\UserAccount;
use Flower\Core\Repository\Eloquent\BaseRepository;

class UserAccountRepository extends BaseRepository
{
    public function model()
    {
        return UserAccount::class;
    }

    /**
     * 新增用户账户
     * @param $user_id
     * @param $adcode
     * @param $ranking
     * @param $share
     * @return mixed
     */
    public function createUserAccount($user_id,$adcode,$ranking,$share)
    {
        $new_user_account_data = [
            'user_id'=>$user_id,
            'share_amount' => 0,
            'share_amount_withdraw' => 0,
            'bonus_amount' => 0,
            'bonus_amount_withdraw' => 0,
            'city' => $adcode,
            'ranking' => $ranking,
            'share' => $share,
            'status' => 1
        ];
        return $this->create($new_user_account_data);
    }

    /**
     * 创建店铺创建合伙人
     * @param $user_id
     * @param $adcode
     * @param $ranking
     * @param $share
     * @return mixed
     */
    public function createPartner($user_id,$adcode,$ranking,$share)
    {
        $data = [
            'city'=>$adcode,
            'ranking'=>$ranking,
            'share'=>$share,
        ];
        return $this->model->where('user_id',$user_id)->update($data);
    }



    /**
     * 根据用户id获取合伙人
     * @param $user_id
     * @return mixed
     */
    public function getUserAccountByUserId($user_id)
    {
        return $this->findByField(['user_id'=>$user_id])->first();
    }

    /**
     * 获取同一地区的合伙人
     * @param $adcode
     * @return mixed
     */
    public function getUserAccountListByAdcode($adcode)
    {
        return $this->findByField(['city'=>$adcode]);
    }


    /**
     * 邀请成功加钱
     * @param $user_id
     * @param $money
     * @return mixed
     */
    public function addMoneyByUserId($user_id,$money)
    {
        return $this->findByField(['user_id'=>$user_id])->first()->increment('share_amount', $money);
    }
}